package com.example.hasee.test.fragment;


import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.hasee.test.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class twoFragment extends Fragment {

    private RecyclerView recyclerview;
    private List<String> dataSource = new ArrayList<>();

    static int[] images = new int[]{R.drawable.special_1, R.drawable.special_2, R.drawable.special_3, R.drawable.special_4, R.drawable.special_5};


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_two, container, false);
        for (int i = 0; i < 10; i++) {
            dataSource.add(i + "");
        }
        initView(view);
        RecyclerViewAdaper recyclerViewAdaper = new RecyclerViewAdaper(getContext(), images);
        recyclerview.setAdapter(recyclerViewAdaper);
        return view;
    }

    private void initView(View view) {
        recyclerview  = view.findViewById(R.id.recyclerview);

        recyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
    }
    static class RecyclerViewAdaper extends RecyclerView.Adapter<RecyclerViewAdaper.ViewHodle>{


        @Override
        public RecyclerViewAdaper.ViewHodle onCreateViewHolder(ViewGroup viewGroup, int i) {
            LayoutInflater inflater = LayoutInflater.from(context);
            View view = inflater.inflate(R.layout.itme_view, null);
            RecyclerViewAdaper.ViewHodle viewHodle = new RecyclerViewAdaper.ViewHodle(view);
            return viewHodle;
        }

        @Override
        public void onBindViewHolder(RecyclerViewAdaper.ViewHodle viewHodle, int position) {
            viewHodle.textView.setBackgroundResource(images[position]);
            Picasso.with(context).load(images[position]).into(viewHodle.textView);

        }

        @Override
        public int getItemCount() {
            return images.length;
        }
        Context context;
        int[] images;

        public RecyclerViewAdaper(Context context, int[] images) {
            this.context = context;
            this.images = images;
        }

        List<String> dataSource;
        static class ViewHodle extends RecyclerView.ViewHolder {
            ImageView textView;
            public ViewHodle(View itemView) {
                super(itemView);
                textView = itemView.findViewById(R.id.item_text);
            }
        }
    }
}




